package com.health.system.javacommon.base;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.util.Assert;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.health.system.javacommon.util.ConvertRegisterHelper;
import com.health.system.javacommon.util.PageRequestFactory;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseStruts2Action extends ActionSupport implements RequestAware {
	protected Map requestMap = null;
	//protected Map paramMap = null;
	
	static {
		//注册converters
		ConvertRegisterHelper.registerConverters();
	}
	
	public void copyProperties(Object target,Object source) {
		BeanUtils.copyProperties(target, source);
	}

	public <T> T copyProperties(Class<T> destClass,Object orig) {
		return BeanUtils.copyProperties(destClass, orig);
	}
	
	public void setRequest(Map request) {
		this.requestMap = request;
	}

	public void savePage(Page page,PageRequest pageRequest){
		savePage("",page,pageRequest);
	}
	
	/**
	 * 用于一个页面有多个extremeTable是使用
	 * @param tableId 等于extremeTable的tableId属性
	 */
	public void savePage(String tableId,Page page,PageRequest pageRequest){
		Assert.notNull(tableId,"tableId must be not null");
		Assert.notNull(page,"page must be not null");
		
		getRequest().setAttribute(tableId+"page", page);
		getRequest().setAttribute(tableId+"totalRows", new Integer(page.getTotalCount()));
		getRequest().setAttribute(tableId+"pageRequest", pageRequest);
	}
	
	public PageRequest newPageRequest(String defaultSortColumns){
		return PageRequestFactory.newPageRequest(ServletActionContext.getRequest(), defaultSortColumns);
    }
	
	public boolean isNullOrEmptyString(Object o) {
		return ObjectUtils.isNullOrEmptyString(o);
	}
	
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ServletActionContext.getRequest();
		// 处理所有参数
		java.util.Enumeration ep = request.getParameterNames();

		while(ep.hasMoreElements()){
		String paramName = (String)ep.nextElement();
		String paramValue = (String)request.getParameter(paramName);
			System.out.println(paramName + " = " + paramValue);
		}

		//获取请求头信息

		java.util.Enumeration eh = request.getHeaderNames();
		      while (eh.hasMoreElements()) {
		          String name = (String)eh.nextElement();
		          String value = request.getHeader(name);
		          System.out.println(name + " = " + value);
		      }


		return request;
	}
	
	public Map getParamMap() {
		Map paramMap = new HashMap();
		HttpServletRequest request = ServletActionContext.getRequest();
		// 处理所有参数
		java.util.Enumeration ep = request.getParameterNames();
		while (ep.hasMoreElements()) {
			String paramName = (String) ep.nextElement();
			String paramValue = (String) request.getParameter(paramName);
			System.out.println("this is debug:"+paramName+" : "+paramValue);
			paramMap.put(paramName, paramValue);
		}
		
		return paramMap;
	}
	
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
}

